package com.hspedu.newData.dmsxl.树.二叉树的遍历;

import com.hspedu.newData.dmsxl.ds.Node;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-13 
 * @Version: 1.0
 */

// 层序遍历，效率不高
public class LC559N叉树的最大深度 {

    public int maxDepth1(Node root) {

        if (root == null) return 0;

        Queue<Node> queue = new ArrayDeque<>();

        queue.offer(root);

        int count = 0;
        while (!queue.isEmpty()){
            int len = queue.size();
            count++;
            while (len>0){
                Node node = queue.poll();

                List<Node> children = node.children;
                for (Node child : children) {
                    queue.offer(child);
                }
                len--;
            }
        }

        return count;
    }


    public int maxDepth(Node root) {

        if (root == null) return 0;

        int h = 0;

        for (Node child : root.children) {
            h = Math.max(h,maxDepth(child));
        }
        return h+1;
    }
}
